#pragma warning(disable:4786)  // Disable debug info warning message (for <map>)
#include "QRM_BasicUtilities.h"
//-------------------------------------------------------------------------
//         Global Variable Definitions
map<int, vector<int> > map_serial;
map<int, int> map_ymFromSerial;
map<int, bool> map_holidays_us;
int day_table[2][13] = {	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
									{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };
bool serialDatesInitialized;
bool holidaysInitialized;
int MAX_VALID_DATE;
int MIN_VALID_DATE;
//-------------------------------------------------------------------------
int initSerialDates()
{
	if (serialDatesInitialized) return (1);

	const int yrs = 137;  //  Must count the number of rows entered below !!

	// Load serial dates for first day of each month
	int raw[yrs][13] = 
	{ {1989,32509,32540,32568,32599,32629,32660,32690,32721,32752,32782,32813,32843},
	 {1990,32874,32905,32933,32964,32994,33025,33055,33086,33117,33147,33178,33208},
	 {1991,33239,33270,33298,33329,33359,33390,33420,33451,33482,33512,33543,33573},
	 {1992,33604,33635,33664,33695,33725,33756,33786,33817,33848,33878,33909,33939},
	 {1993,33970,34001,34029,34060,34090,34121,34151,34182,34213,34243,34274,34304},
	 {1994,34335,34366,34394,34425,34455,34486,34516,34547,34578,34608,34639,34669},
	 {1995,34700,34731,34759,34790,34820,34851,34881,34912,34943,34973,35004,35034},
	 {1996,35065,35096,35125,35156,35186,35217,35247,35278,35309,35339,35370,35400},
	 {1997,35431,35462,35490,35521,35551,35582,35612,35643,35674,35704,35735,35765},
	 {1998,35796,35827,35855,35886,35916,35947,35977,36008,36039,36069,36100,36130},
	 {1999,36161,36192,36220,36251,36281,36312,36342,36373,36404,36434,36465,36495},
	 {2000,36526,36557,36586,36617,36647,36678,36708,36739,36770,36800,36831,36861},
	 {2001,36892,36923,36951,36982,37012,37043,37073,37104,37135,37165,37196,37226},
	 {2002,37257,37288,37316,37347,37377,37408,37438,37469,37500,37530,37561,37591},
	 {2003,37622,37653,37681,37712,37742,37773,37803,37834,37865,37895,37926,37956},
	 {2004,37987,38018,38047,38078,38108,38139,38169,38200,38231,38261,38292,38322},
	 {2005,38353,38384,38412,38443,38473,38504,38534,38565,38596,38626,38657,38687},
	 {2006,38718,38749,38777,38808,38838,38869,38899,38930,38961,38991,39022,39052},
	 {2007,39083,39114,39142,39173,39203,39234,39264,39295,39326,39356,39387,39417},
	 {2008,39448,39479,39508,39539,39569,39600,39630,39661,39692,39722,39753,39783},
	 {2009,39814,39845,39873,39904,39934,39965,39995,40026,40057,40087,40118,40148},
	 {2010,40179,40210,40238,40269,40299,40330,40360,40391,40422,40452,40483,40513},
	 {2011,40544,40575,40603,40634,40664,40695,40725,40756,40787,40817,40848,40878},
	 {2012,40909,40940,40969,41000,41030,41061,41091,41122,41153,41183,41214,41244},
	 {2013,41275,41306,41334,41365,41395,41426,41456,41487,41518,41548,41579,41609},
	 {2014,41640,41671,41699,41730,41760,41791,41821,41852,41883,41913,41944,41974},
	 {2015,42005,42036,42064,42095,42125,42156,42186,42217,42248,42278,42309,42339},
	 {2016,42370,42401,42430,42461,42491,42522,42552,42583,42614,42644,42675,42705},
	 {2017,42736,42767,42795,42826,42856,42887,42917,42948,42979,43009,43040,43070},
	 {2018,43101,43132,43160,43191,43221,43252,43282,43313,43344,43374,43405,43435},
	 {2019,43466,43497,43525,43556,43586,43617,43647,43678,43709,43739,43770,43800},
	 {2020,43831,43862,43891,43922,43952,43983,44013,44044,44075,44105,44136,44166},
	 {2021,44197,44228,44256,44287,44317,44348,44378,44409,44440,44470,44501,44531},
	 {2022,44562,44593,44621,44652,44682,44713,44743,44774,44805,44835,44866,44896},
	 {2023,44927,44958,44986,45017,45047,45078,45108,45139,45170,45200,45231,45261},
	 {2024,45292,45323,45352,45383,45413,45444,45474,45505,45536,45566,45597,45627},
	 {2025,45658,45689,45717,45748,45778,45809,45839,45870,45901,45931,45962,45992},
	 {2026,46023,46054,46082,46113,46143,46174,46204,46235,46266,46296,46327,46357},
	 {2027,46388,46419,46447,46478,46508,46539,46569,46600,46631,46661,46692,46722},
	 {2028,46753,46784,46813,46844,46874,46905,46935,46966,46997,47027,47058,47088},
	 {2029,47119,47150,47178,47209,47239,47270,47300,47331,47362,47392,47423,47453},
	 {2030,47484,47515,47543,47574,47604,47635,47665,47696,47727,47757,47788,47818},
	 {2031,47849,47880,47908,47939,47969,48000,48030,48061,48092,48122,48153,48183},
	 {2032,48214,48245,48274,48305,48335,48366,48396,48427,48458,48488,48519,48549},
	 {2033,48580,48611,48639,48670,48700,48731,48761,48792,48823,48853,48884,48914},
	 {2034,48945,48976,49004,49035,49065,49096,49126,49157,49188,49218,49249,49279},
	 {2035,49310,49341,49369,49400,49430,49461,49491,49522,49553,49583,49614,49644},
	 {2036,49675,49706,49735,49766,49796,49827,49857,49888,49919,49949,49980,50010},
	 {2037,50041,50072,50100,50131,50161,50192,50222,50253,50284,50314,50345,50375},
	 {2038,50406,50437,50465,50496,50526,50557,50587,50618,50649,50679,50710,50740},
	 {2039,50771,50802,50830,50861,50891,50922,50952,50983,51014,51044,51075,51105},
	 {2040,51136,51167,51196,51227,51257,51288,51318,51349,51380,51410,51441,51471},
	 {2041,51502,51533,51561,51592,51622,51653,51683,51714,51745,51775,51806,51836},
	 {2042,51867,51898,51926,51957,51987,52018,52048,52079,52110,52140,52171,52201},
	 {2043,52232,52263,52291,52322,52352,52383,52413,52444,52475,52505,52536,52566},
	 {2044,52597,52628,52657,52688,52718,52749,52779,52810,52841,52871,52902,52932},
	 {2045,52963,52994,53022,53053,53083,53114,53144,53175,53206,53236,53267,53297},
	 {2046,53328,53359,53387,53418,53448,53479,53509,53540,53571,53601,53632,53662},
	 {2047,53693,53724,53752,53783,53813,53844,53874,53905,53936,53966,53997,54027},
	 {2048,54058,54089,54118,54149,54179,54210,54240,54271,54302,54332,54363,54393},
	 {2049,54424,54455,54483,54514,54544,54575,54605,54636,54667,54697,54728,54758},
	 {2050,54789,54820,54848,54879,54909,54940,54970,55001,55032,55062,55093,55123},
	 {2051,55154,55185,55213,55244,55274,55305,55335,55366,55397,55427,55458,55488},
	 {2052,55519,55550,55579,55610,55640,55671,55701,55732,55763,55793,55824,55854},
	 {2053,55885,55916,55944,55975,56005,56036,56066,56097,56128,56158,56189,56219},
	 {2054,56250,56281,56309,56340,56370,56401,56431,56462,56493,56523,56554,56584},
	 {2055,56615,56646,56674,56705,56735,56766,56796,56827,56858,56888,56919,56949},
	 {2056,56980,57011,57040,57071,57101,57132,57162,57193,57224,57254,57285,57315},
	 {2057,57346,57377,57405,57436,57466,57497,57527,57558,57589,57619,57650,57680},
	 {2058,57711,57742,57770,57801,57831,57862,57892,57923,57954,57984,58015,58045},
	 {2059,58076,58107,58135,58166,58196,58227,58257,58288,58319,58349,58380,58410},
	 {2060,58441,58472,58501,58532,58562,58593,58623,58654,58685,58715,58746,58776},
	 {2061,58807,58838,58866,58897,58927,58958,58988,59019,59050,59080,59111,59141},
	 {2062,59172,59203,59231,59262,59292,59323,59353,59384,59415,59445,59476,59506},
	 {2063,59537,59568,59596,59627,59657,59688,59718,59749,59780,59810,59841,59871},
	 {2064,59902,59933,59962,59993,60023,60054,60084,60115,60146,60176,60207,60237},
	 {2065,60268,60299,60327,60358,60388,60419,60449,60480,60511,60541,60572,60602},
	 {2066,60633,60664,60692,60723,60753,60784,60814,60845,60876,60906,60937,60967},
	 {2067,60998,61029,61057,61088,61118,61149,61179,61210,61241,61271,61302,61332},
	 {2068,61363,61394,61423,61454,61484,61515,61545,61576,61607,61637,61668,61698},
	 {2069,61729,61760,61788,61819,61849,61880,61910,61941,61972,62002,62033,62063},
	 {2070,62094,62125,62153,62184,62214,62245,62275,62306,62337,62367,62398,62428},
	 {2071,62459,62490,62518,62549,62579,62610,62640,62671,62702,62732,62763,62793},
	 {2072,62824,62855,62884,62915,62945,62976,63006,63037,63068,63098,63129,63159},
	 {2073,63190,63221,63249,63280,63310,63341,63371,63402,63433,63463,63494,63524},
	 {2074,63555,63586,63614,63645,63675,63706,63736,63767,63798,63828,63859,63889},
	 {2075,63920,63951,63979,64010,64040,64071,64101,64132,64163,64193,64224,64254},
	 {2076,64285,64316,64345,64376,64406,64437,64467,64498,64529,64559,64590,64620},
	 {2077,64651,64682,64710,64741,64771,64802,64832,64863,64894,64924,64955,64985},
	 {2078,65016,65047,65075,65106,65136,65167,65197,65228,65259,65289,65320,65350},
	 {2079,65381,65412,65440,65471,65501,65532,65562,65593,65624,65654,65685,65715},
	 {2080,65746,65777,65806,65837,65867,65898,65928,65959,65990,66020,66051,66081},
	 {2081,66112,66143,66171,66202,66232,66263,66293,66324,66355,66385,66416,66446},
	 {2082,66477,66508,66536,66567,66597,66628,66658,66689,66720,66750,66781,66811},
	 {2083,66842,66873,66901,66932,66962,66993,67023,67054,67085,67115,67146,67176},
	 {2084,67207,67238,67267,67298,67328,67359,67389,67420,67451,67481,67512,67542},
	 {2085,67573,67604,67632,67663,67693,67724,67754,67785,67816,67846,67877,67907},
	 {2086,67938,67969,67997,68028,68058,68089,68119,68150,68181,68211,68242,68272},
	 {2087,68303,68334,68362,68393,68423,68454,68484,68515,68546,68576,68607,68637},
	 {2088,68668,68699,68728,68759,68789,68820,68850,68881,68912,68942,68973,69003},
	 {2089,69034,69065,69093,69124,69154,69185,69215,69246,69277,69307,69338,69368},
	 {2090,69399,69430,69458,69489,69519,69550,69580,69611,69642,69672,69703,69733},
	 {2091,69764,69795,69823,69854,69884,69915,69945,69976,70007,70037,70068,70098},
	 {2092,70129,70160,70189,70220,70250,70281,70311,70342,70373,70403,70434,70464},
	 {2093,70495,70526,70554,70585,70615,70646,70676,70707,70738,70768,70799,70829},
	 {2094,70860,70891,70919,70950,70980,71011,71041,71072,71103,71133,71164,71194},
	 {2095,71225,71256,71284,71315,71345,71376,71406,71437,71468,71498,71529,71559},
	 {2096,71590,71621,71650,71681,71711,71742,71772,71803,71834,71864,71895,71925},
	 {2097,71956,71987,72015,72046,72076,72107,72137,72168,72199,72229,72260,72290},
	 {2098,72321,72352,72380,72411,72441,72472,72502,72533,72564,72594,72625,72655},
	 {2099,72686,72717,72745,72776,72806,72837,72867,72898,72929,72959,72990,73020},
	 {2100,73051,73082,73110,73141,73171,73202,73232,73263,73294,73324,73355,73385},
	 {2101,73416,73447,73475,73506,73536,73567,73597,73628,73659,73689,73720,73750},
	 {2102,73781,73812,73840,73871,73901,73932,73962,73993,74024,74054,74085,74115},
	 {2103,74146,74177,74205,74236,74266,74297,74327,74358,74389,74419,74450,74480},
	 {2104,74511,74542,74571,74602,74632,74663,74693,74724,74755,74785,74816,74846},
	 {2105,74877,74908,74936,74967,74997,75028,75058,75089,75120,75150,75181,75211},
	 {2106,75242,75273,75301,75332,75362,75393,75423,75454,75485,75515,75546,75576},
	 {2107,75607,75638,75666,75697,75727,75758,75788,75819,75850,75880,75911,75941},
	 {2108,75972,76003,76032,76063,76093,76124,76154,76185,76216,76246,76277,76307},
	 {2109,76338,76369,76397,76428,76458,76489,76519,76550,76581,76611,76642,76672},
	 {2110,76703,76734,76762,76793,76823,76854,76884,76915,76946,76976,77007,77037},
	 {2111,77068,77099,77127,77158,77188,77219,77249,77280,77311,77341,77372,77402},
	 {2112,77433,77464,77493,77524,77554,77585,77615,77646,77677,77707,77738,77768},
	 {2113,77799,77830,77858,77889,77919,77950,77980,78011,78042,78072,78103,78133},
	 {2114,78164,78195,78223,78254,78284,78315,78345,78376,78407,78437,78468,78498},
	 {2115,78529,78560,78588,78619,78649,78680,78710,78741,78772,78802,78833,78863},
	 {2116,78894,78925,78954,78985,79015,79046,79076,79107,79138,79168,79199,79229},
	 {2117,79260,79291,79319,79350,79380,79411,79441,79472,79503,79533,79564,79594},
	 {2118,79625,79656,79684,79715,79745,79776,79806,79837,79868,79898,79929,79959},
	 {2119,79990,80021,80049,80080,80110,80141,80171,80202,80233,80263,80294,80324},
	 {2120,80355,80386,80415,80446,80476,80507,80537,80568,80599,80629,80660,80690},
	 {2121,80721,80752,80780,80811,80841,80872,80902,80933,80964,80994,81025,81055},
	 {2122,81086,81117,81145,81176,81206,81237,81267,81298,81329,81359,81390,81420},
	 {2123,81451,81482,81510,81541,81571,81602,81632,81663,81694,81724,81755,81785},
	 {2124,81816,81847,81876,81907,81937,81968,81998,82029,82060,82090,82121,82151},
	 {2125,82182,82213,82241,82272,82302,82333,82363,82394,82425,82455,82486,82516}
	};
	// End load

	MIN_VALID_DATE = 32509;
	MAX_VALID_DATE = 82516+30;
	vector<int> yvec;
	map_serial.clear();
	yvec.clear();
	int month_index;
	for (int yy=0; yy<yrs; yy++)
	{
		yvec.clear();
		yvec.push_back(0);
		for (int mm=1; mm<13; mm++) 
		{
			yvec.push_back(raw[yy][mm]);
			month_index = monthIndex(raw[yy][0], mm);
			map_ymFromSerial[ raw[yy][mm] ] = month_index;
		}
		map_serial[ raw[yy][0] ] = yvec;
	}

	int offScale = 99999;
	map_ymFromSerial[ offScale ] = 0;
	offScale = -99999;
	map_ymFromSerial[ offScale ] = 0;

	serialDatesInitialized = true;
	return (1);
}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------
int initHolidays()
{
	if (holidaysInitialized) return (1);

	const int yrs = 30;  //  Must count the number of rows entered below !!

	// Load serial dates for first day of each month
	int raw[yrs][13] = {
		{1990, 32874, 32923, 33021, 33058, 33119, 33199, 33200, 33231, 33232, 0, 0, 0},
		{1991, 33239, 33287, 33326, 33385, 33423, 33483, 33570, 33597, 0, 0, 0, 0},
		{1992, 33604, 33651, 33711, 33749, 33788, 33854, 33889, 33934, 33963, 0, 0, 0},
		{1993, 33970, 34015, 34068, 34120, 34155, 34218, 34253, 34284, 34298, 34327, 0, 0},
		{1994, 34351, 34386, 34451, 34484, 34519, 34582, 34617, 34649, 34662, 34694, 0, 0},
		{1995, 34701, 34715, 34750, 34803, 34848, 34884, 34946, 35026, 35058, 0, 0, 0},
		{1996, 35065, 35079, 35114, 35160, 35212, 35250, 35310, 35352, 35380, 35397, 0, 0},
		{1997, 35431, 35450, 35478, 35517, 35576, 35615, 35674, 35716, 35745, 35761, 35789, 0},
		{1998, 35796, 35814, 35842, 35895, 35940, 35979, 36045, 36080, 36110, 36125, 36154, 0},
		{1999, 36161, 36178, 36206, 36252, 36311, 36346, 36409, 36444, 36475, 36489, 36518, 0},
		{2000, 36542, 36577, 36637, 36675, 36711, 36773, 36808, 36853, 36885, 0, 0, 0},
		{2001, 36892, 36906, 36941, 36994, 37039, 37076, 37137, 37172, 37207, 37217, 37250, 0},
		{2002, 37257, 37277, 37305, 37344, 37403, 37441, 37501, 37543, 37571, 37588, 37615, 0},
		{2003, 37622, 37641, 37669, 37729, 37767, 37806, 37865, 37907, 37936, 37952, 37980, 0},
		{2004, 37987, 38005, 38033, 38086, 38138, 38173, 38236, 38271, 38302, 38316, 38345, 0},
		{2005, 38369, 38404, 38436, 38502, 38537, 38600, 38635, 38667, 38680, 38712, 0, 0},
		{2006, 38719, 38733, 38768, 38821, 38866, 38902, 38964, 38999, 39044, 39076, 0, 0},
		{2007, 39083, 39097, 39132, 39178, 39230, 39267, 39328, 39363, 39398, 39408, 39441, 0},
		{2008, 39448, 39468, 39496, 39528, 39594, 39633, 39692, 39734, 39763, 39779, 39807, 0},
		{2009, 39814, 39832, 39860, 39913, 39958, 39997, 40063, 40098, 40128, 40143, 40172, 0},
		{2010, 40179, 40196, 40224, 0, 40329, 40364, 40427, 40462, 40493, 40507, 40536, 0},
		{2011, 0, 40560, 40595, 40655, 40693, 40728, 40791, 40826, 40858, 40871, 40903, 0},
		{0,0,0,0,0,0,0,0,0,0,0,0,0}
	};
	// End load

	map_holidays_us.clear();
	for (int yy=0; yy<yrs; yy++)
	{
		if ( raw[yy][0] < 1900) break;

		for (int mm=1; mm<13; mm++) 
			map_holidays_us[ raw[yy][mm] ] = true;
	}

	holidaysInitialized = true;
	return (1);
}
//-------------------------------------------------------------------------------
